const db = wx.cloud.database();
const _ = db.command;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    oid: "",
    QAdetails: {},
    inputText: "",
    userInfo: {}
  },

  //时间戳转换为日期

  add0(m) {
    return m < 10 ? '0' + m : m
  },

  format(shijianchuo) {
    var time = new Date(shijianchuo);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    return y + '-' + this.add0(m) + '-' + this.add0(d);
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      oid: options.ids
    })
    console.log(options.ids);
    db.collection("questionsAnswers")
      .doc(options.ids)
      .get()
      .then(res => {
        console.log(res.data);
        var listss = res.data;
        var t = this.format(listss.releaseTime);
        listss.releaseTime = t;
        for (var i in listss.reply) {
          listss.reply[i].releaseTime = this.format(listss.reply[i].releaseTime);
        }
        this.setData({
          QAdetails: listss
        })
      })
    const ui = wx.getStorageSync("userinfo")
    this.setData({
      userInfo: ui
    })
  },
  doubletxt(e) {
    this.setData({
      inputText: e.detail.value
    })
  },
  // 发送评论
  upReplyBtn() {
    if(this.data.inputText.length==0){
      return;
    }
    const on = {
      userInfo: this.data.userInfo,
      replyTxt: this.data.inputText,
      releaseTime: Date.now()
    }
    db.collection("questionsAnswers")
      .doc(this.data.oid)
      .update({
        data: {
          reply: _.push(on)
        }
      }).then(res => {
        this.setData({
          inputText: ""
        })
        db.collection("questionsAnswers")
          .doc(this.data.oid)
          .get()
          .then(res => {
            // console.log(res);
            var listss = res.data;

            var t = this.format(listss.releaseTime);
            listss.releaseTime = t;

            for (var i in listss.reply) {
              listss.reply[i].releaseTime = this.format(listss.reply[i].releaseTime);
            }

            this.setData({
              QAdetails: listss
            })

          })
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})